package com.njust.service.Impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.njust.dataobject.SystemInfo;
import com.njust.mapper.SystemInfoMapper;
import com.njust.service.SystemInfoService;
import com.njust.utils.DateUtil;
import com.njust.vo.ResultVO;
import com.njust.vo.SystemInfoVO;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class SystemInfoServiceImpl implements SystemInfoService {
    @Autowired
    private SystemInfoMapper systemInfoMapper;
    @Override
    public ResultVO querySystemInfo() {
        //进行方法的一个整改
        Map<String,SystemInfoVO> map = new HashMap<>();
        SystemInfoVO usystemInfoVO = new SystemInfoVO();
        SystemInfoVO dsystemInfoVO = new SystemInfoVO();

         //查询上行传感器状态
        List<SystemInfo> USystemInfo = systemInfoMapper.querySystemInfoULast();
        //查询下行传感器状态
        List<SystemInfo> DSystemInfo = systemInfoMapper.querySystemInfoDLast();
        if(USystemInfo.size()==0 && DSystemInfo.size()==0){
            return ResultVO.createByErrorMessage("无数据");
        }
        SystemInfo uSystemInfo =  USystemInfo.get(USystemInfo.size()-1);//最新一条上行数据
        SystemInfo dSystemInfo = DSystemInfo.get(DSystemInfo.size()-1);//最新一条下行数据
        //将时间类型转化为字符串类型

        usystemInfoVO.setTrainDate(DateUtil.Date2String(uSystemInfo.getTrainDate()));
        usystemInfoVO.setLlaserState(uSystemInfo.getLlaserState());
        usystemInfoVO.setRlaserState(uSystemInfo.getRlaserState());
         usystemInfoVO.setTrainOnlyId(uSystemInfo.getTrainOnlyid());
        usystemInfoVO.setTemState(uSystemInfo.getTempState());
        usystemInfoVO.setPlcState(uSystemInfo.getPlcState());

        dsystemInfoVO.setTrainDate(DateUtil.Date2String(dSystemInfo.getTrainDate()));
        dsystemInfoVO.setLlaserState(dSystemInfo.getLlaserState());
        dsystemInfoVO.setRlaserState(dSystemInfo.getRlaserState());
        dsystemInfoVO.setTrainOnlyId(dSystemInfo.getTrainOnlyid());
        dsystemInfoVO.setTemState(dSystemInfo.getTempState());
        dsystemInfoVO.setPlcState(dSystemInfo.getPlcState());
        map.put("up",usystemInfoVO);
        map.put("down",dsystemInfoVO);
        return ResultVO.createBySuccess(map);
    }
}
